package com.qf.controller;

import com.github.pagehelper.PageInfo;
import com.qf.bean.Admin;
import com.qf.bean.Orders;
import com.qf.bean.Orderstate;
import com.qf.service.AdminService;
import com.qf.service.OrdersService;
import com.qf.service.OrderstateService;
import com.qf.utils.ExcelUtil;
import com.qf.utils.ResultVO;
import enums.PageSizeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 2019/12/9 0009
 * Administrator
 * 江朝西暮，湖晨南曦
 */
@Controller
@RequestMapping("/orders")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderstateService orderstateService;
    @GetMapping("findAll")
    public String findAll(@RequestParam(defaultValue = "1") Integer pageIndex,  Integer pageSize,
                          @RequestParam(defaultValue = "") String str,@RequestParam(defaultValue = "-1")Integer order_state,
                          @RequestParam(defaultValue = "")String startDate, @RequestParam(defaultValue ="")String endDate, Model model){

        PageInfo<Orders> pageInfo = ordersService.findAll(pageIndex, PageSizeEnum.PAGESIZE.getPagesize(),str,order_state,startDate,endDate);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("str",str);
        model.addAttribute("state",order_state);
        model.addAttribute("startDate",startDate);
        model.addAttribute("endDate",endDate);
        List<Orderstate> allStates = orderstateService.findAllStates();
        model.addAttribute("allStates",allStates);
        return "index";
    }
////    登录
//    @PostMapping("/login")
//    @ResponseBody
//    public ResultVO login(String username,String password){
//
//        SecurityUtils.getSecurityManager();
//        Subject subject=SecurityUtils.getSubject();
//
//        try {
//            subject.login(new UsernamePasswordToken(username,password));
//        } catch (AuthenticationException e) {
//            e.printStackTrace();
//            return new ResultVO(899,"用户名或密码错误!!");
//        }
//        return new ResultVO(900,"登录成功!!","");
//
//    }

//    校验时间
    @RequestMapping("/checkTime")
    @ResponseBody
    public ResultVO checkTime(Date startDate){
            if(startDate.getTime() >new Date().getTime()){
                return new ResultVO(89,"起止日期不能大于当前日期!!");
            }
        return new ResultVO(32,"");
    }

//    查询所有预约来获得机构名称
    @RequestMapping("findAllIns")
    public String findAllIns( Model model){
        List<Orders> allOrderIns = ordersService.findAllOrderIns();
        model.addAttribute("allOrderIns",allOrderIns);
        List<Orderstate> allStates = orderstateService.findAllStates();
        model.addAttribute("allStates",allStates);
        return "addOrder";
    }
//    添加预约信息
    @RequestMapping("/addOrder")
    @ResponseBody
    public ResultVO addOrder(Orders orderss){
        orderss.setHandleDate(new Date());
        Integer integer = ordersService.addOrder(orderss);
        System.out.println(orderss);
        if(integer==1){
//            添加成功
            return new ResultVO(0,"添加成功!!");
        }
//        添加失败
        return new ResultVO(222,"添加失败!!");
    }

    @RequestMapping("findOrderByProId")
    public String findOrderByProId(Integer proId,Model model){
//        调用service
//       根据预约编号查询预约的全部信息
        Orders orders = ordersService.findOrderByProId(proId);
        model.addAttribute("orders",orders);
//        查询所有机构名称
        List<Orders> allOrderIns = ordersService.findAllOrderIns();
        model.addAttribute("allOrderIns",allOrderIns);
//        查询所有预约状态
        List<Orderstate> allStates = orderstateService.findAllStates();
        model.addAttribute("allStates",allStates);
        return "updateOrder";
    }


//    修改预约信息
    @RequestMapping("/updateOrdersByProId")
//    Json格式传输数据
    @ResponseBody
    public ResultVO updateOrdersByProId(Orders orderses){
        orderses.setHandleDate(new Date());
        Integer count = ordersService.updateOrdersByProId(orderses);
        if(count==1){
//            修改成功
            return new ResultVO(6,"编辑成功!!");
        }else {
            return new ResultVO(99,"编辑失败!!");
        }

    }

    //  删除预约信息
    @RequestMapping("/deleteOrderByProId")
    @ResponseBody
    public ResultVO deleteOrderByProId(Integer proId){
//        调用service
        Integer count = ordersService.deleteOrderByProId(proId);
        if(count==1){
//            删除成功
            return new ResultVO(11,"删除成功!!");
        }
//        删除失败
        else{
            return new ResultVO(33,"删除失败!!");
        }

    }

    //  批量删除预约信息
    @RequestMapping("deleteOrderByProIds")
    @ResponseBody
    public ResultVO deleteOrderByProIds(int[] proId){
//        调用service
        Integer count = ordersService.deleteOrderByProIds(proId);
        if(count>0){
//            批量删除成功
            return new ResultVO(55,"批量删除成功!!");
        }
//        批量删除失败
        else{
            return new ResultVO(77,"批量删除失败!!");
        }
    }
//    查看预约信息
    @RequestMapping("/seeOrderInfo")
     public String seeOrderInfo(Integer proId,Model model){
        Orders orders = ordersService.findOrderByProId(proId);
        model.addAttribute("orders",orders);
        return "seeOrder";

    }

//    导出数据
    @PostMapping("/toExcel")
    @ResponseBody
    public ResultVO toExcel(int[] proId, HttpServletResponse response) throws IOException {
        List<Orders> orders = ordersService.toExcel(proId);

//        导出数据
            ExcelUtil.createFirstRow("预约信息");
            ExcelUtil.createSheetValue(orders);
//            设置不同的文件夹名字存储导出的数据，防止被覆盖
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");

        String str="D://" + (format.format(new Date())) + ".xls";
        response.setContentType("application/x-download");

        response.addHeader("Content-Disposition", "attachment;filename=" + str);

        File pathFile=new File(str);
        if(!pathFile.getParentFile().exists()){
//            如果本地不存在该文件夹就新创建
            pathFile.getParentFile().mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(pathFile);
//        ExcelUtil.export(new FileOutputStream("D://"+(format.format(new Date()))+".xls"));
        ExcelUtil.export(fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();

        return new ResultVO(666,"导出成功!!，请前往"+pathFile+"查看!!",null);


    }
}


